package com.fh.controller.system.tools;

import com.fh.util.PageData;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chenhao
 * @Title: $file_name$
 * @Param $method_param$
 * @Retrun PUMCH
 * @date 2018/6/21
 */
public class SmsUtlis {


    public static void sendSms(PageData pd) throws ClientProtocolException, IOException {
        String timeString = DataStr();
        String url = "http://www.smsadmin.cn/smsmarketing/wwwroot/api/post_send/";
        String uid = "15040598011"; //用户ID,必须
        String pwd = "leo123456"; //用户密码,必须
        String msg = getMsg(pd);
        //  String msg = pd.getString("username")+"：您于"+timeString+"接收到一条"+pd.getString("name")+"指派给您的配送信息，配送内容如下：配送地址："+pd.getString("address")+" ；配送商品："+pd.getString("goods_name")+"；收件人姓名："+pd.getString("username")+"；收件人电话："+pd.getString("phone")+"；请前往配送。到达配送地点并将商品交付给收件人后，记得前往微信公众号录入订单收货码，才能完成配送。路上注意安全，感谢您的配送【`】";
        String mobile = pd.getString("phone"); //手机号码,必须
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("uid", uid));
        nvps.add(new BasicNameValuePair("pwd", pwd));
        nvps.add(new BasicNameValuePair("mobile", mobile));
        nvps.add(new BasicNameValuePair("msg", msg));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "GB2312"));
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httpPost);

        System.out.println(response.getStatusLine());

        Header[] headers = response.getAllHeaders();
        for (int i = 0; i < headers.length; i++)
            System.out.println(headers[i].getName() + ":"
                    + headers[i].getValue());
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println(EntityUtils.toString(entity, "GB2312"));
        }
    }

    public static String getMsg(PageData pd) {
        String msg = "";
        if ("1".equals(pd.getString("type"))) {
            msg = pd.getString("name") + "：您好，" + pd.getString("context") + "租户的账单已经逾期了，请前往系统进行核对【守护神】";

        } else if ("2".equals(pd.getString("type"))) {
            msg = pd.getString("name") + "：您好，" + pd.getString("context") + "租户的合同已过期，请前往系统进行退租等操作【守护神】";

        }
        else if ("3".equals(pd.getString("type"))) {
            msg = pd.getString("name") + "：您好，" + pd.getString("context") + "租户的合同即将到期，请前往系统进行退租等操作【守护神】";

        }else if ("4".equals(pd.getString("type"))) {
            msg = pd.getString("name") + "：您好，"  + pd.getString("context") +"租户的账单与"  + pd.getString("pay_date") +"日期进行收费，请前往系统进行收费等操作。【守护神】";

        }else if ("5".equals(pd.getString("type"))) {
            msg = pd.getString("context") + "：您好，您有一条账单还有x天需要交费了，账单类型："+pd.getString("bill_name") +"，应付款日："+pd.getString("pay_date") +" ,账单金额："+pd.getString("total_price") +"。【守护神】";

        }else if ("6".equals(pd.getString("type"))) {
            msg = pd.getString("byname") + "：您好，"+pd.getString("business_name")+"商圈"+pd.getString("building_name")+"楼"+pd.getString("room_num")+"项目还有"+pd.getString("end_tx_days")+"天就要到期了，请联系工作人员办理续租/退租业务。【守护神】";

        }else if ("7".equals(pd.getString("type"))) {
            msg = pd.getString("byname") + "：您好，您有一条账单今日进入滞纳金执行日，请尽快付费。账单类型："+ pd.getString("bill_name")+"，应付款日："+ pd.getString("con_end_date")+" ,账单金额："+ pd.getString("total_price")+"。【守护神】";

        } else if ("8".equals(pd.getString("type"))) {
            msg = pd.getString("name") + "：您好，您有待审批的合同需要您审批，请前往系统处理。"+"【守护神】";

        } else if ("9".equals(pd.getString("type"))) {
            msg = pd.getString("name") + "：您好，您有待审批的账单需要您审批，请前往系统处理。"+"【守护神】";

        }
        return msg;
    }

    public static String DataStr() {
        //new日期对象
        Date date = new Date();
//转换提日期输出格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm分");
        System.out.println(dateFormat.format(date));
        return dateFormat.format(date);
    }


    /**
     * @param args
     * @throws IOException
     * @throws ClientProtocolException
     */
    public static void main(String[] args) throws ClientProtocolException, IOException {

        String timeString = DataStr();
        String url = "http://www.smsadmin.cn/smsmarketing/wwwroot/api/post_send/";
        String uid = "15040598011"; //用户ID,必须
        String pwd = "leo123456"; //用户密码,必须
        String msg = "xx：您于" + timeString + "接收到一条xx指派给您的配送信息，配送内容如下：配送地址： ；配送商品：；收件人姓名：；收件人电话：；请前往配送。到达配送地点并将商品交付给收件人后，记得前往微信公众号录入订单收货码，才能完成配送。路上注意安全，感谢您的配送【`】";
        // String msg = "xx：，感谢您的配送【悠逸信息】";


        String mobile = "18500355068"; //手机号码,必须
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("uid", uid));
        nvps.add(new BasicNameValuePair("pwd", pwd));
        nvps.add(new BasicNameValuePair("mobile", mobile));
        nvps.add(new BasicNameValuePair("msg", msg));
        httpPost.setEntity(new UrlEncodedFormEntity(nvps, "GB2312"));
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httpPost);

        System.out.println(response.getStatusLine());

        Header[] headers = response.getAllHeaders();
        for (int i = 0; i < headers.length; i++)
            System.out.println(headers[i].getName() + ":"
                    + headers[i].getValue());
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println(EntityUtils.toString(entity, "GB2312"));
        }
    }


}
